package com.clusterswisdom.horn.api.tmall.action.alipay.pushtrade;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.clusterswisdom.horn.api.db.DbUtil;
import com.clusterswisdom.horn.api.ioc.HornResponse;
import com.clusterswisdom.horn.api.push.PushDataUtil;

public class TestOfPushTotalTrade {
	
	
	private static final String tradeSql = "select count(*) as num ,sum(totalMoney) as totalMoney,sum(countMoney) as countMoney ,sum(feeMoney) as feeMoney,sum(differFee) as differFee ,sum(busShippingAmount) as busShippingAmount from orderaccount where billSpanStrId = ? ";
	
	private static final String bankSql = "select sum(money) as banktotalmoney from transtobank where billspanstrid = ? ";

	private static final String feeSql = "select feecode ,feeName ,sum(feevalue) as feevalue from fee where billSpanStrId = ? group by feecode" ;
	
	private static final String billSpanstrSql = "select * from billspanstr where id = ?";
	public void pushTrade(HornResponse response) throws Exception {
		long billSpanStrId = (Long)response.getData();
		//long billSpanStrId = 21L;
		try {
			JSONObject responseData = getPushDate(billSpanStrId);
			
	
			String result=PushDataUtil.getUtil().pushData("http://uat.tyreplus.cn/tpos.server/tmall/finance/sendTGenAccounts.action", responseData);
			
			//推送前存表操作，真实环境删除
			Connection con = DbUtil.getUtil().getConnection();
			String sql = "insert into pushdata(name , value ,time) values(? ,? ,?)";
			PreparedStatement ps = con.prepareStatement(sql);
			ps.setString(1, "pushTotalTrade"+result+ " uat");
			ps.setString(2,responseData.toString());
			ps.setDate(3, new Date(System.currentTimeMillis()));
			ps.executeUpdate();
			ps.close();
			con.commit();
			
		} finally {
			DbUtil.getUtil().close();
			
		}
	}
	private JSONObject getPushDate(long billSpanStrId) throws SQLException {
		Connection con =DbUtil.getUtil().getConnection();
		
		PreparedStatement ps = con.prepareStatement(tradeSql);
		ps.setLong(1, billSpanStrId);
		ResultSet rs = ps.executeQuery();
		JSONObject result = new JSONObject();
		while(rs.next()){
			result.put("billNum",rs.getInt("num"));
			result.put("totalMoney",Double.parseDouble(pushDataFormater.getDoubleNum(rs.getDouble("totalMoney"))));
			result.put("busShippingAmount", Double.parseDouble(pushDataFormater.getDoubleNum(rs.getDouble("busShippingAmount"))));
			result.put("busSalesAmount", Double.parseDouble(pushDataFormater.getDoubleNum(rs.getDouble("totalMoney"))));
			result.put("busRefundAmount", 0.00);
			double feeMoney = rs.getDouble("feeMoney");
			result.put("countMoney", Double.parseDouble(pushDataFormater.getDoubleNum(rs.getDouble("countMoney")-feeMoney)));
			result.put("feeMoney", Double.parseDouble(pushDataFormater.getDoubleNum(feeMoney)));
			result.put("differFee", Double.parseDouble(pushDataFormater.getDoubleNum(rs.getDouble("differFee"))));
			result.put("busSalesAmount", Double.parseDouble(pushDataFormater.getDoubleNum(rs.getDouble("countMoney"))));

		}
		rs.close();
		
		PreparedStatement billSpanPs = con.prepareStatement(billSpanstrSql);
		billSpanPs.setLong(1, billSpanStrId);
		ResultSet billSpanRs = billSpanPs.executeQuery();
		while(billSpanRs.next()){
			result.put("channel", billSpanRs.getString("channel"));
			result.put("billDateStr", billSpanRs.getString("billDatestr"));
			result.put("billSpanStr", billSpanRs.getString("billSpanStr"));
			result.put("billDate", billSpanRs.getString("billDate"));
			
		}
		billSpanPs.close();
		
		//转到银行卡的钱
		PreparedStatement bankPs = con.prepareStatement(bankSql);
		bankPs.setLong(1, billSpanStrId);
		ResultSet bankRs = bankPs.executeQuery();
		while(bankRs.next()){
			result.put("batchTransBCardAmount", Double.parseDouble(pushDataFormater.getDoubleNum(bankRs.getDouble("banktotalmoney"))));
			
		}
		bankPs.close();
		
		PreparedStatement feePs = con.prepareStatement(feeSql);
		feePs.setLong(1, billSpanStrId);
		ResultSet feeRs = feePs.executeQuery();
		List<JSONObject> feeList = new LinkedList<JSONObject>();
		while(feeRs.next()){
			JSONObject json = new JSONObject();
			json.put("feeValue",Double.parseDouble(pushDataFormater.getDoubleNum(feeRs.getDouble("feevalue"))));
			json.put("feeCode",feeRs.getString("feecode"));
			json.put("feeName",feeRs.getString("feeName"));
			
			feeList.add(json);
		}
		feePs.close();
		result.put("feeList", feeList);
		
		return result;
	}
	
}
